package com.yzz.composeweatherproject.activity

import android.util.Log
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.foundation.lazy.items
import androidx.compose.material.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.AccountCircle
import androidx.compose.material.icons.filled.Home
import androidx.compose.material.icons.filled.Phone
import androidx.compose.runtime.*
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.LayoutDirection
import androidx.compose.ui.unit.dp
import androidx.lifecycle.LifecycleCoroutineScope
import androidx.viewpager.widget.ViewPager
import com.google.accompanist.pager.*
import com.yzz.composeweatherproject.IDataSuccessListener
import com.yzz.composeweatherproject.R
import com.yzz.composeweatherproject.entity.LocationBean
import com.yzz.composeweatherproject.http.okhttp.OkHttpUtils
import dev.chrisbanes.snapper.ExperimentalSnapperApi
import dev.chrisbanes.snapper.SnapperLayoutInfo
import kotlinx.coroutines.launch
import java.util.logging.Logger

/**
 * @Author : yangzuozhe
 * @Time : On 2022/5/29 23:23
 *
 * @Description : CustomBottom
 */


@Composable
fun CustomBottom(
    list: MutableList<CustomBottomType>,
    modifier: Modifier = Modifier,
    click: (Int, CustomBottomType) -> Unit,
) {
    Row(modifier = modifier.fillMaxWidth()) {
        list.forEachIndexed { index, type ->
            Column(Modifier
                .weight(1f)
                .clickable { click.invoke(index, type) },
                horizontalAlignment = Alignment.CenterHorizontally) {
                Image(imageVector = type.icon, contentDescription = "")
                //这里设定底部按钮的样式
                Text("我是${type.name}")
            }
        }
    }
}

data class CustomBottomType(
    val name: String = "",
    val icon: ImageVector = Icons.Default.Home,
    val customBottomType: CustomBottomEnum,
)

enum class CustomBottomEnum {
    HOME,
    CONTENT,
    SETTING
}

var isCreate = false

@Composable
fun CustomHome() {
    var locationData: MutableState<LocationBean?>? = remember {
        mutableStateOf(null)
    }
    Log.d("yyzzhttp", "homestart")

    OkHttpUtils.instance.getLocationUrl(object : IDataSuccessListener<LocationBean> {
        override fun onSuccess(data: LocationBean?) {
            Log.d("yyzzhttp", "home")
            if (data != null) {
                locationData?.value = data
            } else {

            }
        }

        override fun onFail(msg: String?) {
        }

    })


    Box(modifier = Modifier
        .fillMaxSize()
        .background(Color.Red)) {
        Text(text = locationData?.value.toString(), modifier = Modifier.align(Alignment.TopStart))
    }

}

@Composable
fun SootheBottomNavigation(modifier: Modifier = Modifier) {
    var isSelect by remember {
        mutableStateOf(false)
    }
    BottomNavigation(modifier) {
        BottomNavigationItem(
            icon = {
                Icon(
                    imageVector = Icons.Default.Home,
                    contentDescription = null
                )
            },
            label = {
                Text(stringResource(R.string.bottom_navigation_home))
            },
            selected = isSelect,
            onClick = { isSelect = !isSelect }
        )
        BottomNavigationItem(
            icon = {
                Icon(
                    imageVector = Icons.Default.AccountCircle,
                    contentDescription = null
                )
            },
            label = {
                Text(stringResource(R.string.bottom_navigation_profile))
            },
            selected = !isSelect,
            onClick = { isSelect = !isSelect }
        )
    }
}

@Composable
fun CustomContent() {
    Box(modifier = Modifier
        .fillMaxSize()
        .background(Color.Green)) {
        Text(text = "我是内容", modifier = Modifier.align(Alignment.Center))
    }
}

@Composable
fun CustomSetting() {
    Log.d("yyzzsetting", "setting")
    Box(modifier = Modifier
        .fillMaxSize()
        .background(Color.Gray)) {
        Text(text = "我是设置", modifier = Modifier.align(Alignment.Center))
    }
}

@OptIn(ExperimentalPagerApi::class, ExperimentalSnapperApi::class)
@Composable
fun HomePage(
    list: MutableList<CustomBottomType>,
    state: PagerState = rememberPagerState(),
    modifier: Modifier = Modifier,
) {
    HorizontalPager(count = list.size,
        state = state,
        flingBehavior = PagerDefaults.flingBehavior(state = state),
        modifier = modifier) { page ->
        Log.d("yyzzpage","$page")
        if (page == 0) {
            CustomHome()
        } else if (page == 1) {
            CustomContent()
        } else if (page == 2) {
            CustomSetting()
        }
    }
}